<?php
namespace Admin\Controller;
use Think\Controller;
class UploadController extends Controller {
    public function index() {
        switch ($_POST['type']) {
            case '1': $sub = '/pic';break;
            case '2': $sub = '/flv';break;
            case '3': $sub = '/swf';break;
        }
        $config = array(
            'maxSize' => 0,
            'rootPath' => './',
            'savePath' => './Public/Uploads',
            'saveName' => 'time',
            'exts' => array('jpg', 'gif', 'png', 'jpeg', 'bmp', 'flv', 'swf'),
            'autoSub' => true,
            'subName' => $sub,
        );
        $upload = new \Think\Upload($config);
        $info = $upload -> uploadOne($_FILES['file']);
        if(!$info) {// 上传错误提示错误信息
            $data['message'] = $upload->getError();
            $data['flag'] = false;
//            echo $upload->getError();
            $this->ajaxReturn($data);
        }else{// 上传成功 获取上传文件信息
            $info['savepath'] = str_replace('./Public','',$info['savepath']);
            $res = D('Resource');
            $data['path'] = $info['savepath'].$info['savename'];
            $data['type'] = intval($_POST['type']);
            if ($res->create($data))
                $id = $res->add();
            if ($id) {
                $data['flag'] = true;
                $data['id'] = $id;
                $data['type'] = $info['ext'];
                $this->ajaxReturn($data);
            }
        }
    }
}